<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class home_model extends CI_Model
{
    function home_model()
    {
        parent::__construct();
    }
// checking
    function checkLogin()
    {
        if($this->session->userdata("USERNAME") == "")
        {
            unset($_SESSION["CI_LOGIN"]);
            redirect(site_url("login"));
        }
        if(!isset($_SESSION["CI_LOGIN"])) show_error(lang("errorLogin"));
    }
 // Phan trang
    function paging_Admin($urlPage, $total, $numshow=25, $uri_segment=4)
    {
        $this->load->library("pagination");
        $this->pagination->base_url = base_url().$urlPage;
        $this->pagination->total_rows = $total;
        $this->pagination->per_page = $numshow;
        $this->pagination->num_links = 4;
        $this->pagination->uri_segment = $uri_segment;
        return $this->pagination->AdminPaging(); 
    }   
    
// load menu
    private function subMenu($id,$current) // module menu co 2 cap
    {
        $this->db->order_by('weight', 'DESC');
        $query = $this->db->get_where('modules', array('fmn' => $id, 'status' => '1'));   
        if($query->num_rows() == 0) return '';
        $str = '<ul>';
        foreach($query->result_array() as $row)
        {
            $sl = $row["url"] == $current ? "current" : "";
            //$title = $this->admintrans->trans("modules",$row["id"],"title",$row["title"]); // truong hop da ngon ngu
            $title = $row["title"];
            $str .= '<li><a class="'.$sl.'"  href="'.site_url($row["url"]).'" id="menuleft">'.$title.'</a></li>';
        }
		$str .= '</ul>';
        return $str;
    }
    private function getCurr($FMN, $url)// lay current cho menu cha
    {
        $query = $this->db->get_where('modules', array('fmn' => $FMN, 'url' => $url));
        if($query->num_rows() > 0) return "current";
        return "";
    }
    function mainMenu()// module menu co 2 cap
    {
        //$current = uri_string();
        //$current = str_replace('cms/','',$current);
        $current = $this->uri->segment(2);
        $this->db->order_by('weight', 'DESC');
        $query = $this->db->get_where('modules', array('status' => '1', 'fmn' => 0));
        $str = ''; 
        foreach($query->result_array() as $row)
        {
            $nosub = $row["cmn"] == 'No' ? "no-submenu" : "";
            $sl = $row["url"] == $current ? "current" : "";
            if($row["cmn"] == 'Yes') $sl = $this->getCurr($row["id"],$current);
            //$title = $this->admintrans->trans("modules",$row["id"],"title",$row["title"]); // truong hop da ngon ngu
            $title = $row["title"];
            $str .= ' <li> <a href="'.site_url($row["url"]).'"  class="nav-top-item '.$nosub." ".$sl.'"> '.$title.' </a>';
            if($row["cmn"] == 'Yes') $str .= $this->subMenu($row["id"],$current); // KEYid cua menu cha
            else $str .= '</li>';
        }   
        return $str;
    }
}